package cn.lili.modules.system.serviceimpl;


import cn.lili.modules.system.entity.dos.SupplyChainAccess;
import cn.lili.modules.system.mapper.SupplyChainAccessMapper;
import cn.lili.modules.system.mapper.SupplyChainMapper;
import cn.lili.modules.system.service.SupplyChainAccessService;
import cn.lili.modules.system.service.SupplyChainService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SupplyChainAccessServiceImpl
        extends ServiceImpl<SupplyChainAccessMapper,SupplyChainAccess> implements SupplyChainAccessService {



    @Override
    public List<SupplyChainAccess> getListBySupplierId(String supplierId) {
        LambdaQueryWrapper<SupplyChainAccess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplyChainAccess::getUserId,supplierId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Transactional
    @Override
    public int saveChainAccessList(String supplierId, List<String> chainIdList) {
        LambdaQueryWrapper<SupplyChainAccess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplyChainAccess::getUserId,supplierId);
        this.baseMapper.delete(queryWrapper);
        for(String chainId : chainIdList){
            SupplyChainAccess one = new SupplyChainAccess();
            one.setUserId(supplierId);
            one.setSupplyChainId(chainId);
            one.setCreateTime(new Date());
            this.baseMapper.insert(one);
        }
        return chainIdList.size();
    }
}
